package cn.imvc.app.controllers.system;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import cn.imvc.app.entities.Department;
import cn.imvc.app.entities.Department_position;
import cn.imvc.app.entities.Group;
import cn.imvc.app.entities.Position;
import cn.imvc.app.entities.Role;
import cn.imvc.app.entities.User;
import cn.imvc.app.models.DepartmentModel;
import cn.imvc.app.models.GroupModel;
import cn.imvc.app.models.PositionDepartmentModel;
import cn.imvc.app.models.RoleModel;
import cn.imvc.app.models.UserModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class UserControllerImpl implements UserController {
	public Page<User> page;
	public ViewRequest viewRequest;
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}
	public User user;
	
	public void setUser(User user) {
		this.user = user;
	}
	public int id;
	public int[] ids;
	public String password;
	public int[] group;
	public int[] role;
	
	public Department department;
	public int groupid;
	public int roleid;
	
	public Group g;
	public Role r;
	
	

	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}
	public Map<String, String> userGroup=new HashMap<String, String>();
	
	public Map<String, String> userRole=new HashMap<String, String>();
	
	public List<Group>  groups;
	public List<Role> roles;
	
	public List<Department> deplist;
	public List<Position> poslist;
	
	
	
	public List<Department> getDeplist() {
		return deplist;
	}

	public void setDeplist(List<Department> deplist) {
		this.deplist = deplist;
	}

	public List<Position> getPoslist() {
		return poslist;
	}

	public void setPoslist(List<Position> poslist) {
		this.poslist = poslist;
	}
	
	
	@Autowired
	private UserModel userModel;
	@Autowired
	private GroupModel groupModel;
	@Autowired
	private RoleModel roleModel;
	@Autowired
	private DepartmentModel departmentModel;
	@Autowired
	PositionDepartmentModel positionDepartmentModel;
	
	public String search() {
		roles=roleModel.find();
		if(roleid!=0)
		r=roleModel.get(roleid);
		return "user/search";
	}

	public String index() {
		groups=groupModel.find();

		if(groupid!=0)
		g=groupModel.get(groupid);

		if(viewRequest==null){
			viewRequest=new ViewRequest();
			Map<String, String> params=new HashMap<String, String>();
			viewRequest.setParams(params);
		}
		LinkedHashMap<String, String> sort=new LinkedHashMap<String, String>();
		sort.put("u.id", "desc");
		viewRequest.setSort(sort);
		
		String group_sql="";
		if(groupid!=0){
			group_sql="join u.group g";
			viewRequest.getParams().put("_and_g_dot_id_eq_number", String.valueOf(groupid));
		}
		
		String role_sql="";
		if(roleid!=0){
			role_sql="join u.role r";
			viewRequest.getParams().put("_and_r_dot_id_eq_number", String.valueOf(roleid));
		}
		
		String department_sql="";
		if(department!=null){
			department_sql="join u.department_position dp join dp.department d";
			viewRequest.getParams().put("_and_d_dot_id_eq_number", department.getCode());
		}
		page=userModel.find("distinct u","User u "+group_sql+" "+department_sql+" "+role_sql+" ",viewRequest);
		return "user/index";
	}

	public String show() {
		groups=groupModel.find();
		roles=roleModel.find();

		user=userModel.get(id);
		for (Object object : user.getGroup()) {
			Group group=(Group)object;
			userGroup.put(group.getCode(),group.getName());
		}

		for (Object object : user.getRole()) {
			Role role=(Role)object;
			userRole.put(role.getCode(), role.getName());
		}
		
		return "user/show";
	}

	public String editNew() {
		groups=groupModel.find();
		roles=roleModel.find();

		return "user/editNew";
	}

	public String create() {
		
		if(password.equals(user.getPassword())&&!password.equals("")){
			Md5PasswordEncoder md5pe=new Md5PasswordEncoder();
			md5pe.setEncodeHashAsBase64(true);
		    user.setPassword(md5pe.encodePassword(password, "imvc"));
		    userModel.save(user);
		    
		    if(group!=null)
		    for (int gid : group) {
		    	Group group=groupModel.get(gid);
		    	group.getUser().add(user);
		    	user.getGroup().add(group);
		    }
		    
		    if(role!=null)
		    for (int rid : role) {
		    	Role role=roleModel.get(rid);
		    	role.getUser().add(user);
		    	user.getRole().add(role);
		    }

		    if(deplist!=null&&poslist!=null&&deplist.size()==poslist.size())
		    for (int i = 0; i < deplist.size(); i++) {
		    	Set<Department_position> dps=positionDepartmentModel.get(Integer.parseInt(deplist.get(i).getCode()),Integer.parseInt(poslist.get(i).getCode()));
		    	for (Department_position department_position : dps) {
		    		department_position.getUser().add(user);
		    		user.getDepartment_position().add(department_position);
				}
		    }
		    
		    userModel.save(user);
		}else{
			return Render.json(new DialogBean("300", "两次密码输入不一致","user", "closeCurrent"));
		}
		return Render.json(new DialogBean("200", "创建成功","user", "closeCurrent"));
	}

	public String edit() {
		groups=groupModel.find();
		roles=roleModel.find();

		user=userModel.get(id);
		for (Object object : user.getGroup()) {
			Group group=(Group)object;
			userGroup.put(group.getCode(),group.getName());
		}

		for (Object object : user.getRole()) {
			Role role=(Role)object;
			userRole.put(role.getCode(), role.getName());
		}
		
		return "user/edit";
	}

	public String update() {

		if(!password.equals(""))
		if(password.equals(user.getPassword())){
			Md5PasswordEncoder md5pe=new Md5PasswordEncoder();
			md5pe.setEncodeHashAsBase64(true);
		    user.setPassword(md5pe.encodePassword(password, "imvc"));
		}else{
			return Render.json(new DialogBean("300", "两次密码输入不一致","user", "closeCurrent"));
		}
		
		User loaduser=userModel.get(id);
		
		for (Object object : loaduser.getGroup()) {
			Group group=(Group)object;
			group.getUser().remove(loaduser);
		}
		loaduser.getGroup().clear();
		
		for (Object object : loaduser.getRole()) {
			Role role=(Role)object;
			role.getUser().remove(loaduser);
		}
		loaduser.getRole().clear();
		
		for (Object object : loaduser.getDepartment_position()) {
			Department_position department_position=(Department_position)object;
			department_position.getUser().remove(loaduser);
		}
		loaduser.getDepartment_position().clear();
		
		userModel.save(loaduser);
		
	    if(group!=null)
	    for (int gid : group) {
	    	Group group=groupModel.get(gid);
	    	group.getUser().add(loaduser);
	    	loaduser.getGroup().add(group);
		}
	    
	    if(role!=null)
	    for (int rid : role) {
	    	Role role=roleModel.get(rid);
	    	role.getUser().add(loaduser);
	    	loaduser.getRole().add(role);
		}
	 
	    if(deplist!=null&&poslist!=null&&deplist.size()==poslist.size())
		    for (int i = 0; i < deplist.size(); i++) {
		    	Set<Department_position> dps=positionDepartmentModel.get(Integer.parseInt(deplist.get(i).getCode()),Integer.parseInt(poslist.get(i).getCode()));
		    	for (Department_position department_position : dps) {
		    		department_position.getUser().add(loaduser);
		    		loaduser.getDepartment_position().add(department_position);
				}
		    }

	    
	    userModel.save(loaduser); 
	
	    userModel.update(id, user);
	    
		return Render.json(new DialogBean("200", "修改成功","user", "closeCurrent"));

	}

	public String destroy() {
		
		user=userModel.get(id);
		for (Object object : user.getGroup()) {
			Group group=(Group)object;
			group.getUser().remove(user);
		}
		user.getGroup().clear();
		for (Object object : user.getRole()) {
			Role role=(Role)object;
			role.getUser().remove(user);
		}
		user.getRole().clear();
		
		for (Object object : user.getDepartment_position()) {
			Department_position department_position=(Department_position)object;
			department_position.getUser().remove(user);
		}
		user.getDepartment_position().clear();
		
		userModel.save(user);
		
		userModel.delete(id);
		return Render.json(new DialogBean("200", "删除成功","user"));

	}

	public String destroyBatch() {
		
		for (int id : ids) {
			user=userModel.get(id);
			for (Object object : user.getGroup()) {
				Group group=(Group)object;
				group.getUser().remove(user);
			}
			user.getGroup().clear();
			for (Object object : user.getRole()) {
				Role role=(Role)object;
				role.getUser().remove(user);
			}
			user.getRole().clear();
			
			for (Object object : user.getDepartment_position()) {
				Department_position department_position=(Department_position)object;
				department_position.getUser().remove(user);
			}
			user.getDepartment_position().clear();
			
			userModel.save(user);
			
			userModel.delete(id);
		}
		return Render.json(new DialogBean("200", "删除成功","user"));

	}

}
